﻿
using CLVisionCommu;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           CLVisionCommuCaller.Init(3,null, out int ErrorCode, out string ErrorText, out bool _);

        }
      
        
        private void button2_Click(object sender, EventArgs e)
        {
            Task.Run(() =>
            {
                ushort oldValue = 0;
                ushort up = 0;
                ushort down = 0;
                while (true)
                {

                    double value;
                    CLVisionCommuCaller.ReceiveConstantData(0, "StartSingle", out value, out int _, out string _, out bool _);
                  
                    // ModbusCallerTS.ReadFromPin("StartSingle", out int _, out string _, out bool _, out value);
                    if (oldValue == 0 && (oldValue ^ Convert.ToInt32(value)) == 1)
                    {
                        oldValue = Convert.ToUInt16(value);
                        up += 1;
                        CLVisionCommuCaller.SendConstantData(0, "ResultStates", up, out int _, out string _, out bool _);
                    }
                    if (oldValue == 1 && (oldValue ^ Convert.ToInt32(value)) == 1)
                    {
                        oldValue = Convert.ToUInt16(value);
                        down += 1;
                        CLVisionCommuCaller.ReceiveConstantData(0, "Para2", out value, out int _, out string _, out bool _);
                        CLVisionCommuCaller.ReceiveConstantData(0, "Command", out value, out int _, out string _, out bool _);
                        CLVisionCommuCaller.ReceiveConstantData(0, "Para1", out value, out int _, out string _, out bool _);

                        CLVisionCommuCaller.SendConstantData(0, "RunStates", Convert.ToInt32(value), out int _, out string _, out bool _);
                        CLVisionCommuCaller.SendConstantData(0, "TestplanStates", down, out int _, out string _, out bool _);
                    }
                }
            });
            Task.Run(() =>
            {
                ushort oldValue = 0;
                ushort up = 0;
                ushort down = 0;
                while (true)
                {

                    double value;
                    CLVisionCommuCaller.ReceiveConstantData(1, "StartSingle", out value, out int _, out string _, out bool _);
                    // ModbusCallerTS.ReadFromPin("StartSingle", out int _, out string _, out bool _, out value);
                    if (oldValue == 0 && (oldValue ^ Convert.ToInt32(value)) == 1)
                    {
                        oldValue = Convert.ToUInt16(value);
                        up += 1;
                        CLVisionCommuCaller.SendConstantData(01, "ResultStates", up, out int _, out string _, out bool _);
                    }
                    if (oldValue == 1 && (oldValue ^ Convert.ToInt32(value)) == 1)
                    {
                        oldValue = Convert.ToUInt16(value);
                        down += 1;
                        CLVisionCommuCaller.ReceiveConstantData(1, "Para2", out value, out int _, out string _, out bool _);
                        CLVisionCommuCaller.ReceiveConstantData(1, "Command", out value, out int _, out string _, out bool _);
                        CLVisionCommuCaller.ReceiveConstantData(1, "Para1", out value, out int _, out string _, out bool _);

                        CLVisionCommuCaller.SendConstantData(1, "RunStates", Convert.ToInt32(value), out int _, out string _, out bool _);
                        CLVisionCommuCaller.SendConstantData(1, "TestplanStates", down, out int _, out string _, out bool _);
                     
                    }
                }
            });
            Task.Run(() =>
            {
                ushort oldValue = 0;
                ushort up = 0;
                ushort down = 0;
                while (true)
                {

                    double value;
                    CLVisionCommuCaller.ReceiveConstantData(2, "StartSingle", out value, out int _, out string _, out bool _);
                    // ModbusCallerTS.ReadFromPin("StartSingle", out int _, out string _, out bool _, out value);
                    if (oldValue == 0 && (oldValue ^ Convert.ToInt32(value)) == 1)
                    {
                        oldValue = Convert.ToUInt16(value);
                        up += 1;
                        CLVisionCommuCaller.SendConstantData(2, "ResultStates", up, out int _, out string _, out bool _);
                    }
                    if (oldValue == 1 && (oldValue ^ Convert.ToInt32(value)) == 1)
                    {
                        oldValue = Convert.ToUInt16(value);
                        down += 1;
                        CLVisionCommuCaller.ReceiveConstantData(2, "Para2", out value, out int _, out string _, out bool _);
                        CLVisionCommuCaller.ReceiveConstantData(2, "Command", out value, out int _, out string _, out bool _);
                        CLVisionCommuCaller.ReceiveConstantData(2, "Para1", out value, out int _, out string _, out bool _);

                        CLVisionCommuCaller.SendConstantData(2, "RunStates", Convert.ToInt32(value), out int _, out string _, out bool _);
                        CLVisionCommuCaller.SendConstantData(2, "TestplanStates", down, out int _, out string _, out bool _);
                    }
                }
            });
        }

        private void button3_Click(object sender, EventArgs e)
        {
            CLVisionCommuCaller.ReceiveConstantData(1, "Command", out double value, out int _, out string _, out bool _);
            CLVisionCommuCaller.SendConstantData(1, "ResultStates", 19, out int _, out string _, out bool _);

        }

        private void button4_Click(object sender, EventArgs e)
        {

        }
    }
}
